package com.fy.servlet;

import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import com.fy.util.QrcodeUtil;

@WebServlet("*.qrcode")
public class QrcodeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@Override
	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String path = request.getRequestURI();
		if (path.contains("create.qrcode")) {
			String message = request.getParameter("message");
			String fileName = "qr_" + new Date().getTime() + ".png";
			String imgPath = request.getSession().getServletContext().getRealPath("/") + "img/qrcode/" + fileName;
			QrcodeUtil.encodeQrcode(message, imgPath, "");
			request.getSession().setAttribute("imgsrc", "img/qrcode/" + fileName);
			request.getRequestDispatcher("/WEB-INF/page/qrcode/showQrcode.jsp").forward(request, response);
		} else if (path.contains("show.qrcode")) {
			String imgsrc = request.getSession().getAttribute("imgsrc").toString();
			response.getWriter().print(imgsrc);
		} else if (path.contains("vcard.qrcode")) {
			String content = request.getParameter("content");
			String fileName = "qx_" + new Date().getTime() + ".png";
			String imgPath = request.getSession().getServletContext().getRealPath("/") + "img/qrcode/" + fileName;
			String frontPath = request.getSession().getServletContext().getRealPath("/") + "img/qrcode/front.png";
			QrcodeUtil.encodeQrcode(content, imgPath, frontPath);
			response.getWriter().print("img/qrcode/" + fileName);
		}
	}
}